package net.neptech.service;
import com.alibaba.nls.client.protocol.NlsClient;
import com.alibaba.nls.client.protocol.SampleRateEnum;
import com.alibaba.nls.client.protocol.asr.SpeechTranscriber;
import com.alibaba.nls.client.protocol.asr.SpeechTranscriberListener;
import com.alibaba.nls.client.protocol.asr.SpeechTranscriberResponse;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

@Service
@RequiredArgsConstructor
public class AsrService {

    private final NlsClient nlsClient;
    private final WebSocketClientService wsClientService;

    // 存储每个会议的 ASR 会话
    private final Map<String, SpeechTranscriber> activeTranscriptions = new ConcurrentHashMap<>();

//    public void processAudioChunk(String conferenceId, byte[] audioData) {
//        SpeechTranscriber trans = activeTranscriptions.get(conferenceId);
//        if (trans == null) {
//            startAsr(conferenceId);
//            trans = activeTranscriptions.get(conferenceId);
//        }
//        if (trans != null) {
//            trans.send(audioData); // 发送音频到阿里云
//        }
//    }

//    public void startAsr(String conferenceId) {
//        try {
//            SpeechTranscriber trans = new SpeechTranscriber(nlsClient, new MyTransListener(conferenceId, wsClientService));
//            // 设置参数
//            trans.setAppKey("your-asr-app-key");
////            trans.setFormat("PCM"); // 你的音频是 PCM
//            trans.setFormat(com.alibaba.nls.client.protocol.InputFormatEnum.PCM);
////            trans.setSampleRate(16000);
//            trans.setSampleRate(SampleRateEnum.SAMPLE_RATE_16K);
//            trans.setEnableIntermediateResult(true); // 开启中间结果
//            trans.start();
//            activeTranscriptions.put(conferenceId, trans);
//        } catch (Exception e) {
//            System.err.println("启动 ASR 失败: " + e.getMessage());
//        }
//    }

    public void stopAsr(String conferenceId) {
        SpeechTranscriber trans = activeTranscriptions.remove(conferenceId);
        if (trans != null) {
            try {
                trans.stop();
            } catch (Exception e) {
                System.err.println("停止 ASR 失败: " + e.getMessage());
            }
        }
    }
//    private static class MyTransListener extends SpeechTranscriberListener {
//        private final String conferenceId;
//        private final WebSocketClientService wsClientService;
//
//        public MyTransListener(String conferenceId, WebSocketClientService wsClientService) {
//            this.conferenceId = conferenceId;
//            this.wsClientService = wsClientService;
//        }
//
//        @Override
//        public void onTranscriptionResultChanged(TranscriptionResult result) {
//            String text = result.getText();
//            if (text != null && !text.trim().isEmpty()) {
//                wsClientService.pushRealTimeTranscript(conferenceId, text, result.isPartial());
//            }
//        }
//
//        @Override
//        public void onChannelClosed(ChannelCloseEvent event) {
//            System.out.println("ASR 通道关闭: " + conferenceId);
//        }
//
//        // 新增必须实现的方法
//        @Override
//        public void onTranscriberStart(SpeechTranscriberResponse response) {
//            System.out.println("ASR 识别开始: " + conferenceId);
//        }
//
//        // 可根据需要重写其他回调方法，例如 onError、onTaskFailed 等
//    }
//
//    // 回调监听器
////    private static class MyTransListener extends SpeechTranscriberListener {
////        private final String conferenceId;
////        private final WebSocketClientService wsClientService;
////
////        public MyTransListener(String conferenceId, WebSocketClientService wsClientService) {
////            this.conferenceId = conferenceId;
////            this.wsClientService = wsClientService;
////        }
////
////        @Override
////        public void onTranscriptionResultChanged(TranscriptionResult result) {
////            String text = result.getText();
////            if (text != null && !text.trim().isEmpty()) {
////                wsClientService.pushRealTimeTranscript(conferenceId, text, result.isPartial());
////            }
////        }
////
////        @Override
////        public void onChannelClosed(ChannelCloseEvent event) {
////            System.out.println("ASR 通道关闭: " + conferenceId);
////        }
////    }
//
//    // 回调监听器
////    private static class MyTransListener extends SpeechTranscriberListener {
////        private final String conferenceId;
////        private final WebSocketClientService wsClientService;
////
////        public MyTransListener(String conferenceId, WebSocketClientService wsClientService) {
////            this.conferenceId = conferenceId;
////            this.wsClientService = wsClientService;
////        }
////
////        @Override
////        public void onTranscriptionResultChanged(TranscriptionResult result) {
////            String text = result.getText();
////            if (text != null && !text.trim().isEmpty()) {
////                wsClientService.pushRealTimeTranscript(conferenceId, text, result.isPartial());
////            }
////        }
////
////        @Override
////        public void onChannelClosed(ChannelCloseEvent event) {
////            System.out.println("ASR 通道关闭: " + conferenceId);
////        }
////    }
}